﻿using System.Collections.Generic;
using Common;
using Common.EventHandler;
using Domain.Common;
using Domain.Map;
using MapComponentSprite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace UIComponentSprite.SmallMap
{
    public class SmallMapSprite : UserControlledSprite
    {
        /// <summary>
        /// 地图尺寸倍数，放大或缩小
        /// </summary>
        public static float MapSizeMultiple=1f;//默认1倍，可根据窗口大小设置此值
        List<MapbuildingSprite> mMapbuildingSprites=new List<MapbuildingSprite>();
        public SmallMapSprite(MapCloseButtonSprite sprite,MapOpenButtonSprite sprite2)
        {
            sprite.ClickLeftEvent += new ClickLeftEventHandler(ChangeShowing);
            sprite2.ClickLeftEvent += new ClickLeftEventHandler(ChangeShowing);
            this.ClickLeftEvent += new ClickLeftEventHandler(MapJump);
            DrawPosition.Width = (int) (Map.MapDimensions.X * MapSizeMultiple);
            DrawPosition.Height = (int) (Map.MapDimensions.Y * MapSizeMultiple);
            foreach (var b in Domain.Building.Buildings)
            {
                MapbuildingSprite mapbuilding=new MapbuildingSprite(b.Value.Position);
                mapbuilding.ChangeShowing(null);
                mMapbuildingSprites.Add(mapbuilding);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (!Showing)
                return;
            SpriteBatch.Draw(this.Image, DrawPosition,Color.White);
            foreach (var mapbuildingSprite in mMapbuildingSprites)
            {
                mapbuildingSprite.Draw(gameTime);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (!Showing)
                return;
            LoadPic();
            DrawPosition.X = GameScreen.ViewportSize.X - DrawPosition.Width;
            DrawPosition.Y = GameScreen.ViewportSize.Y - DrawPosition.Height; ;
            foreach (var mapbuildingSprite in mMapbuildingSprites)
            {
                mapbuildingSprite.Update(gameTime);
            }
            ClickLeftEventListener(null);
         
        }
        /// <summary>
        /// 大地图跳转
        /// </summary>
        private void MapJump(object mousePosition)
        {
            Point point=new Point(MouseState.X,MouseState.Y);
            point.X = this.DrawPosition.Width-(GameScreen.ViewportSize.X - point.X);
            point.Y = this.DrawPosition.Height - (GameScreen.ViewportSize.Y - point.Y);
            MapBackgroundLayerSprite.MapJumpToDestination(point);
        }
        protected override void LoadPic()
        {
            if (this.Image == null)
                this.Image = Game.Content.Load<Texture2D>(@"Images\smallMap\smallMap");
           
        }
    }
}
